package guda.house.biz.form.bill;

import guda.house.biz.util.PriceUtil;
import guda.house.dal.dataobject.AccountEstateDO;
import org.springframework.util.StringUtils;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class AccountEstateForm {

    @NotNull(message = "不能为空")
    private Long villageId;
    @NotNull(message = "不能为空")
    private String gmtAccount;
    @NotNull(message = "不能为空")
    @Pattern(regexp = "[+]?(([1-9]\\d*[.]?)|(0.))(\\d{0,2})?",message = "金额应该是整数,最多2位数的小数")
    private String accountMoney;
    @NotNull(message = "不能为空")
    @Size(min = 0, max = 10, message = "长度在0到10个字符之间")
    private String accountType;
    @Size(min = 0, max = 100, message = "长度在0到100个字符之间")
    private String accountComment;

    public AccountEstateDO toDO() {
        AccountEstateDO accountEstateDO = new AccountEstateDO();
        accountEstateDO.setVillageId(villageId);
        accountEstateDO.setAccountComment(accountComment);
        accountEstateDO.setAccountMoney(PriceUtil.convertPriceYuan(accountMoney));
        accountEstateDO.setAccountType(accountType);

        if (StringUtils.hasText(gmtAccount)) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                Date parse = simpleDateFormat.parse(gmtAccount);
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(parse);
                calendar.set(Calendar.HOUR_OF_DAY, 0);
                calendar.set(Calendar.MINUTE, 0);
                calendar.set(Calendar.SECOND, 0);
                calendar.set(Calendar.MILLISECOND, 0);
                accountEstateDO.setGmtAccount(calendar.getTime());
            } catch (ParseException e) {
                throw new RuntimeException(e);
            }

        }
        return accountEstateDO;
    }


    public Long getVillageId() {
        return villageId;
    }

    public void setVillageId(Long villageId) {
        this.villageId = villageId;
    }

    public String getGmtAccount() {
        return gmtAccount;
    }

    public void setGmtAccount(String gmtAccount) {
        this.gmtAccount = gmtAccount;
    }

    public String getAccountMoney() {
        return accountMoney;
    }

    public void setAccountMoney(String accountMoney) {
        this.accountMoney = accountMoney;
    }

    public String getAccountType() {
        return accountType;
    }

    public void setAccountType(String accountType) {
        this.accountType = accountType;
    }

    public String getAccountComment() {
        return accountComment;
    }

    public void setAccountComment(String accountComment) {
        this.accountComment = accountComment;
    }
}
